using System;
using System.Windows.Forms;
using System.Drawing;

namespace RideMe.Presentation
{
    /// <summary>
    /// Summary description for ColorComboBox.
    /// </summary>
    public class ColorComboBox : ComboBox
    {
        private SolidBrush _blackBrush;
        private SolidBrush _whiteBrush;

        private Color _selectedColor;

        /// <summary>
        /// Get the selected color from the combo box
        /// </summary>
        public Color SelectedColor
        {
            get
            {
                return _selectedColor;
            }
            set
            {
                if (_selectedColor != value)
                {
                    _selectedColor = value;
                    SetSelectedColor(_selectedColor.Name);
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ColorComboBox"/> class.
        /// </summary>
        public ColorComboBox()
        {
            _blackBrush = new SolidBrush(Color.Black);
            _whiteBrush = new SolidBrush(Color.White);

            // Get all the values from the KnownColor enumeration.
            System.Array colorsArray = Enum.GetValues(typeof(KnownColor));
            KnownColor[] allColors = new KnownColor[colorsArray.Length];

            Array.Copy(colorsArray, allColors, colorsArray.Length);

            for (int i = 0; i < allColors.Length - 1; i++)
            {
                this.Items.Add(allColors[i].ToString());
            }

            this.BackColor = Color.White;
            _selectedColor = Color.Black;
            this.Text = _selectedColor.Name;

            this.DrawMode = DrawMode.OwnerDrawFixed;
            DropDownStyle = ComboBoxStyle.DropDownList;

            HookEvents();

        }

        /// <summary>
        /// Hooks up events.
        /// </summary>
        private void HookEvents()
        {
            this.DrawItem += new DrawItemEventHandler(OnDrawItem);
            this.SelectedIndexChanged += new System.EventHandler(OnSelectedIndexChanged);
            this.DropDown += new System.EventHandler(OnDropDown);
        }

        /// <summary>
        /// Unhooks events (called on dispose).
        /// </summary>
        private void UnhookEvents()
        {
            this.DrawItem -= new DrawItemEventHandler(OnDrawItem);
            this.SelectedIndexChanged -= new System.EventHandler(OnSelectedIndexChanged);
            this.DropDown -= new System.EventHandler(OnDropDown);
        }

        /// <summary>
        /// Called when [draw item].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> instance containing the event data.</param>
        private void OnDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            Graphics graphics = e.Graphics;

            if (e.Index == -1)
            {
                return;
            }

            Color brushColor = Color.FromName((string)this.Items[e.Index]);
            SolidBrush brush = new SolidBrush(brushColor);

            e.DrawBackground();
            e.DrawFocusRectangle();

            Rectangle rect;
            rect = e.Bounds;
            rect.X += 5;
            rect.Y += 2;
            rect.Width = 20;
            rect.Height = 10;

            graphics.FillRectangle(brush, rect);
            graphics.DrawRectangle(Pens.Black, rect);
            rect.Offset(25, 0);
            rect.Height = 15;
            rect.Width = 150;
            graphics.DrawString((string)this.Items[e.Index], e.Font, _blackBrush, rect);
        }

        /// <summary>
        /// Called when [selected index changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void OnSelectedIndexChanged(object sender, System.EventArgs e)
        {
            _selectedColor = Color.FromName((string)this.SelectedItem);
        }

        /// <summary>
        /// prevents the hightlighted text being shown when drop down
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        private void OnDropDown(object sender, System.EventArgs e)
        {
            _selectedColor = Color.FromName((string)this.SelectedItem);
        }


        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="T:System.Windows.Forms.ComboBox"></see> and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            UnhookEvents();
        }

        /// <summary>
        /// Selects the appropriate item in the list.
        /// </summary>
        /// <param name="colorName">Name of the color.</param>
        private void SetSelectedColor(string colorName)
        {
            int foundItem = this.FindString(colorName);

            if (foundItem != -1)
            {
                this.SelectedIndex = foundItem;
            }
        }

    }
}
